using UnityEngine;

public class CarSlot : EventCommander, ISlot
{
	[SerializeField]
	private EntityBehavior behavior;

	[SerializeField]
	private PrivateCarManager carManager;

	private UITexture _texture;

	private bool loadImage = true;

	private bool flag = true;

	public UITexture Texture
	{
		get
		{
			if (_texture == null)
			{
				_texture = GetComponent<UITexture>();
			}
			if (_texture == null)
			{
				_texture = base.gameObject.AddComponent<UITexture>();
			}
			_texture.alpha = 0f;
			_texture.enabled = false;
			return _texture;
		}
	}

	public EventCommander eventCommander
	{
		get
		{
			return this;
		}
	}

	public string slotName
	{
		get
		{
			return base.name;
		}
	}

	public string elementName
	{
		get
		{
			return behavior.name;
		}
	}

	public float interfaceAnimationTime
	{
		get
		{
			return carManager.culldown;
		}
	}

	public Transform slotTransform
	{
		get
		{
			return base.transform;
		}
	}

	public EntityBehavior car
	{
		get
		{
			return behavior;
		}
	}

	public override void Init()
	{
		base.Init();
		InitEvents(new string[6] { "SlotSellected", "TextureLoaded", "ValueChanged", "AlternativeText", "StartInterfaceAnimation", "StopInterfaceAnimation" });
	}

	public override void Awake()
	{
		base.Awake();
		if (GameController.instance.curTypeGame == TypeGame.Zombe)
		{
			WalkWindow.instance.carSelector.gameObject.SetActive(false);
		}
		carManager = Object.FindObjectOfType<PrivateCarManager>();
		if (TutorialManager.TuturialWorking())
		{
			WalkWindow.instance.carSelector.forceBlock = true;
			WalkWindow.instance.carSelector.SetVisible(false);
		}
	}

	private new void Start()
	{
		if (car == null)
		{
			WalkWindow.instance.carSelector.forceBlock = true;
			WalkWindow.instance.carSelector.SetVisible(false);
		}
	}

	public void SetActive(bool val)
	{
		if (flag)
		{
			flag = false;
		}
		else if (PrivateCarManager.instance.CallCar())
		{
			RiseEvent(this, "StartInterfaceAnimation", interfaceAnimationTime, true);
		}
	}

	public void SetNewCar(EntityBehavior car)
	{
		if ((bool)WalkWindow.instance)
		{
			WalkWindow.instance.carSelector.forceBlock = car == null;
			if (GameController.instance.curTypeGame == TypeGame.WalkingRobots)
			{
				if (car != null && car is TitanBehavior)
				{
					WalkWindow.instance.carSelector.forceBlock = true;
					WalkWindow.instance.carSelector.SetVisible(false);
				}
				else
				{
					WalkWindow.instance.carSelector.SetVisible(car != null);
				}
			}
			else
			{
				WalkWindow.instance.carSelector.SetVisible(car != null);
			}
			WalkWindow.instance.carSelector.SetSlot(slotName, this);
		}
		behavior = car;
		if (!(car == null) && (bool)GameTopWindow.instance && loadImage)
		{
			LoadAndAddTexture(car);
		}
	}

	public void LoadAndAddTexture(EntityBehavior curCar)
	{
		if (!(curCar.nameSpritePreview == string.Empty))
		{
			Object @object = Resources.Load("AtlasGame/AG_" + curCar.GetCurrentSkin().name);
			Texture texture = null;
			texture = ((!(@object == null)) ? (@object as Texture) : (Resources.Load("AtlasGame/" + curCar.nameSpritePreview) as Texture));
			Texture.mainTexture = texture;
			RiseEvent(this, "TextureLoaded", Texture);
			RiseEvent(this, "SlotSellected", this);
		}
	}

	public void StopAnimation()
	{
		RiseEvent(this, "StopInterfaceAnimation");
	}

	public override void OnEnable()
	{
		if (GameController.instance != null && GameController.instance.currentMapType == mapType.Small)
		{
			base.gameObject.SetActive(false);
		}
	}
}
